package com.tongda.core.service;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tongda.core.common.Constants;
import com.tongda.core.utils.CommunicationUtil;
import com.tongda.core.vo.RestNews;
import com.tongda.core.vo.Restaurant;

import android.util.Log;

public class RestaurantService {

	/*
	 * 获取符合ip地址餐厅的第一个用户当做cookie
	 */
	public static String getCookie() {
		String jsonStr = CommunicationUtil.get(Constants.BASE_SERVIER_URL
				+ "mobile/index/getCookie");
		Log.e("URL", Constants.BASE_SERVIER_URL);
		Log.e("COOKIE", jsonStr);
		return jsonStr;
	}

	/*
	 * 获取餐厅信息
	 */
	public static Restaurant getRestInfo() {
		Restaurant rest = new Restaurant();
		String jsonStr = CommunicationUtil.get(Constants.BASE_SERVIER_URL
				+ "mobile/table/getRstInfo");
		String restName = "";
		List<String> restPicUrls = null;
		String notes = "";
		String consPerPerson = "";
		String diningEnvSb = "";
		String specialEnvSb = "";
		try {
			JSONObject obj = new JSONObject(jsonStr);
			restName = obj.getString("restName");

			restPicUrls = new ArrayList<String>();
			JSONArray restPicArray = new JSONArray(obj.getString("restPicUrls"));
			for (int i = 0; i < restPicArray.length(); i++) {
				restPicUrls.add(restPicArray.getString(i));
			}

			notes = obj.getString("restNotes");
			consPerPerson = obj.getString("consPerPerson");
			diningEnvSb = obj.getString("diningEnvStr");
			specialEnvSb = obj.getString("specialEnvSb");

		} catch (JSONException e) {
			e.printStackTrace();
		}

		rest.setRestName(restName);
		rest.setRestPicUrls(restPicUrls);
		rest.setNotes(notes);
		rest.setConsPerPerson(consPerPerson);
		rest.setDiningEnvSb(diningEnvSb);
		rest.setSpecialEnvSb(specialEnvSb);
		return rest;
	}

	/*
	 * 获取餐厅动态信息
	 */
	public static List<RestNews> getRestNews() {
		List<RestNews> restNewsList = new ArrayList<RestNews>();
		String jsonStr = CommunicationUtil.get(Constants.BASE_SERVIER_URL
				+ "mobile/table/getRestNews");
		try {
			JSONArray jsonArray = new JSONArray(jsonStr);
			for (int i = 0; i < jsonArray.length(); i++) {
				RestNews restNews = new RestNews();
				JSONObject obj = jsonArray.getJSONObject(i);
				restNews.setTitle(obj.getString("title"));
				restNews.setContent(obj.getString("content"));
				restNews.setCreatTimeStr(obj.getString("creatTimeStr"));
				restNews.setCreatMan(obj.getString("creatMan"));
				restNewsList.add(restNews);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return restNewsList;
	}
}
